//配置路由的地方
import Vue from "vue";
import Router from "vue-router";
import Home from "@/pages/home";
import Login from "@/pages/Login";
import Atlas from "@/pages/atlas";
import ArticleDetail from "@/pages/articleDetail"
import UserInfo from "@/pages/user";
//引入路由组件


//使用插件
Vue.use(Router)


 const router = new Router({
    mode: 'history', // 去掉url中的#
    scrollBehavior: () => ({ y: 0 }),
    routes:
    //配置路由
        [
            {
                path:"/",
                redirect:"/home"
            },
            {
                path:"/home",
                component:Home,
                meta: {
                    title: '鳄鱼杆-pole',
                    isAuth:false
                }
            },
            {
                path:"/login",
                name:"login",
                component:Login,
                meta: {
                    title: '欢迎登录',
                    isAuth:false
                }
            },
            {
                path:"/atlas",
                component:Atlas,
                meta: {
                    title: '图集',
                    isAuth:false
                }
            },
            {
                path:"/articleDetail",
                component:ArticleDetail,
                meta: {
                    title: '文章详情',
                    isAuth:false
                }
            },
            {
                path:"/userInfo",
                component:UserInfo,
                meta: {
                    title: '个人中心',
                    isAuth:true
                }
            },
            {
                path:"/article",
                component:()=>import("@/pages/articleList"),
                meta: {
                    title: '全部收藏',
                }
            },
            {
                path: "/404",
                component: () => import(/* webpackChunkName: "404" */ "@/pages/404.vue"),
                meta: {
                    title: "未知领域",
                    hidden: true,
                    notNeedAuth: true }
            },
            {
                path: "*",
                redirect: "/404",
                meta: { hidden: true }
            }

        ]
})



router.beforeEach(async(to, form, next) => {

    //登陆后，不再可以访问login page
    if(localStorage.getItem('pole_time_not' ) !== null && to.name === 'login'){
        next("/")
    }
    else{
        if (!to.meta.isAuth) {
            next()
        }else{
            // 判断 是否登录，登录的可以进去
            if (localStorage.getItem('pole_time_not' ) !== null ) {
                next()  //放行
            } else {
                alert('请登录！')
                next("/login")
            }
        }

        //处理title
        if (to.meta.title) {
            document.title = to.meta.title
        } else {
            document.title = '未知领域' //此处写默认的title
        }

    }
})

// 防止连续点击多次路由报错
let routerPush = Router.prototype.push;
let routerReplace = Router.prototype.replace;
// push
Router.prototype.push = function push(location) {
    return routerPush.call(this, location).catch(err => err)
}
// replace
Router.prototype.replace = function push(location) {
    return routerReplace.call(this, location).catch(err => err)
}


export default router




